package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.CouponCodeMapper;
import com.gxa.pingan.mapper.UserMapper;
import com.gxa.pingan.pojo.CouponCode;
import com.gxa.pingan.service.CouponCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)
public class CouponCodeServiceImpl implements CouponCodeService {
    @Autowired
    private CouponCodeMapper couponCodeMapper = null;

    @Autowired
    private UserMapper userMapper = null;

    @Override
    public void add(CouponCode couponCode) throws BusinessException {
        UUID uuid = UUID.randomUUID();
        String code = uuid.toString().replaceAll("-", "");
        code = code.substring(0,10);
        couponCode.setCode(code);
        couponCode.setGetNum(0L);
        Integer add = couponCodeMapper.add(couponCode);
        if (add == 0) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }

    @Override
    public List<CouponCode> getList(Integer page, Integer size) {
        if (page == null || page == 0) {
            page = 1;
        }
        if (size == null || size == 0) {
            size = 10;
        }
        Integer offset = (page - 1) * size;
        return couponCodeMapper.selectAll(offset,size);
    }

    @Override
    public Integer getListCount() {
        return couponCodeMapper.selectAll(null,null).size();
    }

    @Override
    public void update(CouponCode couponCode) throws BusinessException {
        Long id = couponCode.getId();
        System.out.println(id);
        if (couponCode == null || couponCode.getId() == null ) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        CouponCode selectById = couponCodeMapper.selectById(couponCode.getId());
        //抛出异常（记得补上）
        if (selectById == null) {
//            throw new BusinessException(ExceptionCode.)
        }
        Integer update = couponCodeMapper.update(couponCode);
        if (update < 1) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }

    @Override
    public void deleteCpnCode(Long id) throws BusinessException {
        CouponCode selectById = couponCodeMapper.selectById(id);
        if (selectById == null) {
            //抛出异常（记得补上）
//            throw  new
        }
        Integer integer = couponCodeMapper.deleteById(id);
        if (integer < 1) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }

    @Override
    public void sendCode(Long cid, String code, String phone) {

    }

    @Override
    public Boolean checkPhone(String phone) throws BusinessException {
        if (phone == null) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        return userMapper.selectByPhone(phone) != null;
    }

    @Override
    public void addSendCode(String phone, Long cid, String content) throws BusinessException {
        if (phone == null ) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        if (userMapper.selectByPhone(phone) == null ) {
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        if (cid == null) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        CouponCode couponCode = couponCodeMapper.selectById(cid);
        if (couponCode == null) {
            //缺少优惠券兑换码
            throw new BusinessException(ExceptionCode.NO_SUCH_COUPON);
        }
        Integer integer = couponCodeMapper.sendCode(cid, phone, content);
        if (integer == 0) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }
}
